<template>
  <div class="qrcode-sign">
    <el-form :model="form" label-width="120px" @submit.prevent="handleSubmit">
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="form.startTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="YYYY-MM-DDTHH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
          v-model="form.endTime"
          type="datetime"
          placeholder="选择结束时间"
          value-format="YYYY-MM-DDTHH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">创建二维码签到</el-button>
      </el-form-item>
    </el-form>

    <div v-if="qrcodeData" class="qrcode-display">
      <h3>签到二维码</h3>
      <img :src="'data:image/png;base64,' + qrcodeData" alt="签到二维码" />
    </div>

    <el-table :data="activities" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="type" label="类型" width="120">
        <template #default="scope">
          {{ getActivityType(scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间" />
      <el-table-column prop="endTime" label="结束时间" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button 
            type="primary" 
            size="small" 
            @click="getQRCode(scope.row.id)"
            v-if="scope.row.type === 'qr'"
          >
            查看二维码
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { createQRCodeSignActivity, getSignActivityQRCode, getSignActivities } from '@/api/teacher'
import { ElMessage } from 'element-plus'

export default {
  name: 'QRCodeSign',
  setup() {
    const form = ref({
      startTime: '',
      endTime: ''
    })
    const qrcodeData = ref('')
    const activities = ref([])

    const getActivityType = (type) => {
      const types = {
        normal: '普通签到',
        code: '签到码',
        qr: '二维码'
      }
      return types[type] || type
    }

    const handleSubmit = async () => {
      if (!form.value.startTime || !form.value.endTime) {
        ElMessage.warning('请选择开始时间和结束时间')
        return
      }
      try {
        const response = await createQRCodeSignActivity({
          startTime: form.value.startTime,
          endTime: form.value.endTime
        })
        if (response.code === 200) {
          ElMessage.success('二维码签到活动创建成功')
          form.value.startTime = ''
          form.value.endTime = ''
          fetchActivities()
        }
      } catch (error) {
        ElMessage.error('创建失败：' + error.message)
      }
    }

    const getQRCode = async (activityId) => {
      try {
        const response = await getSignActivityQRCode(activityId)
        if (response.code === 200) {
          qrcodeData.value = response.data
        }
      } catch (error) {
        ElMessage.error('获取二维码失败：' + error.message)
      }
    }

    const fetchActivities = async () => {
      try {
        const response = await getSignActivities()
        if (response.code === 200) {
          activities.value = response.data
        }
      } catch (error) {
        ElMessage.error('获取活动列表失败：' + error.message)
      }
    }

    onMounted(() => {
      fetchActivities()
    })

    return {
      form,
      qrcodeData,
      activities,
      handleSubmit,
      getQRCode,
      getActivityType
    }
  }
}
</script>

<style scoped>
.qrcode-sign {
  padding: 20px;
}

.qrcode-display {
  margin-top: 20px;
  text-align: center;
}

.qrcode-display img {
  max-width: 300px;
  margin-top: 10px;
}
</style> 